//
//  PictureSelectView.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/3/23.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "PictureSelectView.h"
#import <AssetsLibrary/AssetsLibrary.h>


#define imageH 60 // 图片高度
#define imageW 70 // 图片宽度
#define kMaxColumn 3 // 每行显示数量
#define MaxImageCount 9 // 最多显示图片个数
#define deleImageWH 25 // 删除按钮的宽高
#define kAdeleImage @"Btn_Normal_Weijiancha.png" // 删除按钮图片
#define kAddImage @"add.png" // 添加按钮图片

@interface PictureSelectView ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    UIScrollView  *imagesScrolleview;
    // 标识被编辑的按钮 -1 为添加新的按钮
    NSInteger editTag;
    
    UIButton *addBtn;
    CGFloat  space;
    UILabel *addLabel;
//    NSMutableArray  *_allPicArray;
    
  
}
@end

@implementation PictureSelectView
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _allPicArray = [NSMutableArray array];
        
        imagesScrolleview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        NSLog(@"%f=====%f",self.width,self.height);
        imagesScrolleview.showsHorizontalScrollIndicator = NO;
        imagesScrolleview.delaysContentTouches = YES;
        [self addSubview:imagesScrolleview];
        if (kScreenWidth == 320 ) {
            space = [UIView getHeight:5.0f];
        }else{
            space = [UIView getHeight:10.0f];
        }
        
        for (int i = 0; i< imagesScrolleview.subviews.count; i++) {
                    id  vi = imagesScrolleview.subviews[i];
                    NSLog(@"%@",[vi class]);
        }
    }
    return self;
}

- (void)setIsHadCheck:(BOOL)isHadCheck
{
    _isHadCheck = isHadCheck;
    if (!_isHadCheck) {//未检查的时候添加这些
        [self addSubviews];
        if(_isShowTinTitle){
            UILabel *titleLabel = [ViewTool getLabelWith:CGRectMake(addBtn.x, addBtn.maxY + 5, 150, 20) WithTitle:@"* 可以上传6张图片，请添加" WithFontSize:12.0f WithTitleColor:GRAYCOLOR WithTextAlignment:NSTextAlignmentCenter];
//            titleLabel.center = CGPointMake(addBtn.center.x, addBtn.maxY + 5);
            [self addSubview:titleLabel];
        }
    }
}

- (void)setIsHideBtn:(BOOL)isHideBtn
{

    if (isHideBtn) {
        addBtn.hidden = YES;
    }else{
        addBtn.hidden = NO;
    }

}

- (void)addSubviews
{
    
    addBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 0, 70 , 70)];
    addBtn = [[UIButton alloc] initWithFrame:CGRectMake(10 , 5 , imageW, imageW)];
    [addBtn bringSubviewToFront:imagesScrolleview];
    addBtn.tag = 15151515;
    [addBtn setImage:[UIImage imageNamed:@"Btn_Normal_paizhangpian.png"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addPic) forControlEvents:UIControlEventTouchUpInside];
//                addBtn.center =  CGPointMake(35, self.height - 60.0f);

    [imagesScrolleview addSubview:addBtn];
    
    addLabel = [ViewTool getLabelWith:CGRectMake(addBtn.maxX + space, addBtn.y + addBtn.height / 2.0f-10, 100, 20) WithTitle:@"添加照片" WithFontSize:16.0f WithTitleColor:UIColorFromRGB(0xe5e5e5) WithTextAlignment:NSTextAlignmentLeft];
    [imagesScrolleview addSubview:addLabel];
    
    
}
- (void)drawRect:(CGRect)rect
{
    for (UIView *vi in imagesScrolleview.subviews) {
        if ([vi isKindOfClass:[UIButton class]]) {
            //
            if(vi.tag != 15151515){
                [vi removeFromSuperview];
            }
            
        }
    }
//    NSLog(@"重绘之前 删除scrllview的图片 子视图剩余%ld",imagesScrolleview.subviews.count);
   //将按到的图片 放到总图片数组里面
   
//#warning 子视图会重叠
//    NSLog(@"需要排布的图片个数===%ld",_allPicArray.count);
    //数组是AlAsset 资源
    //获得等比缩略图 原图的等比缩略图[asset aspectRatioThumbnail];
    //       缩略图是相册中每张照片的poster图  [asset thumbnail];
    if(_allPicArray.count == 0){
        if (addBtn) {
            [UIView animateWithDuration:0.2 animations:^{
                addBtn.center =  CGPointMake(35, self.height - 40.0f);
            }];
        }
        if (addLabel) {
            [UIView animateWithDuration:0.2 animations:^{
                  addLabel.hidden = NO;
            }];
//            addLabel.hidden = NO;
        }
    }
    
    for (int i = 0; i< _allPicArray.count; i++) {
        //        UIImage *chooseImage = [images[i] aspectRatioThumbnail];
        addLabel.hidden = YES;
        NSData *data =_allPicArray[i];
        UIImage *img = [UIImage imageWithData:data];
    
        UIButton *imageView = [[UIButton alloc] initWithFrame:CGRectMake(10 + (imageW + 10) * i, 5 , imageW, imageW)];
        imageView.tag = 1 + i;
        [imageView bringSubviewToFront:imagesScrolleview];
        [imageView setBackgroundImage:img forState:UIControlStateNormal];
//        imageView.backgroundColor = BlueColor;
        imageView.hidden = NO;
        [imageView addTarget:self action:@selector(clickTodelete:) forControlEvents:UIControlEventTouchUpInside];
        [imagesScrolleview addSubview:imageView];
        if (i == _allPicArray.count - 1 && addBtn != nil) {
            CGRect frame = addBtn.frame;
            addBtn.frame = CGRectMake(imageView.maxX + 10, imageView.y, frame.size.width, frame.size.height);
        }
        imagesScrolleview.contentSize = CGSizeMake(addBtn.maxX+ 10, 0);
    }
    
 NSLog(@"view的子视图的个数%ld",imagesScrolleview.subviews.count);
//    for (int i = 0; i< _allPicArray.count   ; i++) {
////        NSLog(@"%@",_allPicArray[i]);
//    }
}
- (void)addPic
{
    if ([self.delegate respondsToSelector:@selector(addImage)]) {
        [self.delegate addImage];
    }
}
- (void)setImages:(NSMutableArray *)images
{
    _images = images;
//    addBtn.hidden = YES;
    
    NSLog(@"新照片加入之前 数组 中的个数 %ld",_allPicArray.count);
    for (int i = 0; i< self.images.count; i++) {
       
        if (_allPicArray.count < 6) {
            [_allPicArray addObject:self.images[i]];
        }
        
    }
//    for (int i = 0; i< _allPicArray.count; i++) {
//        NSLog(@"所有图片的顺序 …%@",_allPicArray[i]);
//    }
    [self setNeedsDisplay];
//    NSLog(@"view的子视图的个数%ld",imagesScrolleview.subviews.count);
}
- (void)clickTodelete:(UIButton *)btn
{
    [btn removeFromSuperview];

    [_allPicArray removeObjectAtIndex:btn.tag - 1];
    self.isHideBtn = NO;
    [self setNeedsDisplay];
}

- (void)setShowImages:(NSMutableArray *)showImages
{
    _showImages = showImages;
    for (int i = 0; i< showImages.count; i++) {
      //展示图片
        
        }
           
}

@end
